package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.base.anno.NeedLogin;
import cn.wolfcode.p2p.business.domain.PaymentScheduleDetail;
import cn.wolfcode.p2p.business.query.CreditTransferQueryObject;
import cn.wolfcode.p2p.business.service.ICreditTransferService;
import cn.wolfcode.p2p.business.service.IPaymentScheduleDetailService;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TransferController {

    @Autowired
    private ICreditTransferService creditTransferService;

    /**
     * 可转让的债权列表
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("listCanTransferCredit")
    public String listCanTransferCredit(Model model, @ModelAttribute("qo") CreditTransferQueryObject qo) {

        //转让人,只查询自己的
        qo.setToLoginInfo(UserContext.getLoginInfo().getId());

        model.addAttribute("pageInfo", creditTransferService.selectForPage(qo));

        return "credittransfer_cantrans_list";
    }

    /**
     * 执行债权转让
     *
     * @return
     */
    @RequestMapping("creditTransfer")
    @ResponseBody
    public JsonResult creditTransfer(Long[] bidId) {

        creditTransferService.creditTransfer(bidId);
        return JsonResult.instance();
    }

    /**
     * 执行认购
     */
    @ResponseBody
    @RequestMapping("subscribe")
    @NeedLogin
    public JsonResult creditTransferInvest(Long id) {

        creditTransferService.subscribe(id);

        return JsonResult.instance();
    }

    /**
     * 转让中的债权
     *
     * @return
     */
    @RequestMapping("listMyTransferCredit")
    @NeedLogin
    public String listMyTransferCredit(Model model, @ModelAttribute("qo") CreditTransferQueryObject qo) {

        qo.setToLoginInfo(UserContext.getLoginInfo().getId());
        qo.setBidRequestState(Constants.TRANSFER_STATE_BIDDING);
        model.addAttribute("pageInfo",  creditTransferService.queryForTransferPage(qo));
        return "credittransfer_mytrans_list";
    }

    /**
     * 撤销自己发布的债权标
     * @param id
     * @return
     */
    @RequestMapping("cancelTransferCredit")
    @ResponseBody
    public JsonResult cancelMyTransferCredit(Long id){

        creditTransferService.cancelCredit(id);
        return JsonResult.instance();
    }

    /**
     * 我认购的债权标
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("listMySubscribeCredit")
    @NeedLogin
    public String listMySubscribeCredit(Model model, @ModelAttribute("qo") CreditTransferQueryObject qo) {

        qo.setToLoginInfo(UserContext.getLoginInfo().getId());
        qo.setBidRequestState(Constants.TRANSFER_STATE_SUBSCRIPTION);
        model.addAttribute("pageInfo",  creditTransferService.queryForSubscribePage(qo));
        return "credittransfer_mysubscribe_list";
    }


}
